/**
 * ObjectBlock.h
 * Coyright (c) 2009 Xiaofeng Gu. All Rights Reserved.
 * Author : Xiaofeng Gu
 *
 * Creation Data : 2009/06/11
 */

#pragma once


// -------------------------------------------------------------------
// IObjectBlock class
// -------------------------------------------------------------------
class IObjectBlock
{
public:
	IObjectBlock() {}
	virtual~ IObjectBlock() {}
	virtual bool		IsPersistence() = 0;
};

// -------------------------------------------------------------------
// CObjectBlock class
// -------------------------------------------------------------------
template<class T>
class CObjectBlock : public IObjectBlock
{
public:
	CObjectBlock(T* pBlockInfo, bool bAutoFree, bool bPersistence)
		: m_pBlockInfo(pBlockInfo),
		  m_bAutoFree(bAutoFree),
		  m_bPersistence(bPersistence) {}

	virtual ~CObjectBlock()
	{
		if (m_bAutoFree)
		{
			delete m_pBlockInfo;
			m_pBlockInfo = NULL;
		}
	}

	T* operator->()
	{
		return m_pBlockInfo;
	}

	/**
	 * Method that returns whether this block object is a persistence object.
	 */
	virtual bool		IsPersistence()
	{ return m_bPersistence; }

	/**
	 * Method that returns the object attached with the block object.
	 */
	T*					GetBlockInfo()
	{
		return m_pBlockInfo;
	}

private:
	T*					m_pBlockInfo;
	bool				m_bAutoFree;
	bool				m_bPersistence;
};